package com.fanfan.SmartPensionSystembackend.mapper;

import com.fanfan.SmartPensionSystembackend.entity.DailyDataBaseInfo;
import com.fanfan.SmartPensionSystembackend.entity.DailyDataDailyData;
import com.fanfan.SmartPensionSystembackend.entity.DailyDataTijianData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Description DailyDataMapper
 * @Author Guangdi Chen
 * @Date 2025-03-19
 */
@Mapper
public interface DailyDataMapper {
    // 获取所有用户信息
    @Select("SELECT e.elderId AS userId, e.admissionTime, e.name, e.gender, e.birthdate, e.bedId, b.bedPlace " +
            "FROM elder e LEFT JOIN bed b ON e.bedId = b.bedId")
    List<DailyDataBaseInfo> getAllDailyData(); // 返回类型改为List<DailyDataBaseInfo>
    // 根据用户ID获取用户信息
    @Select("SELECT e.elderId AS userId, e.admissionTime, e.name, e.gender, e.birthdate, e.bedId, b.bedPlace " +
            "FROM elder e LEFT JOIN bed b ON e.bedId = b.bedId WHERE e.elderId = #{userId}")
    DailyDataBaseInfo screenDailyDataWithId(String userId);
    // 根据地址获取用户信息
    @Select("SELECT e.elderId AS userId, e.admissionTime, e.name, e.gender, e.birthdate, e.bedId, b.bedPlace " +
            "FROM elder e LEFT JOIN bed b ON e.bedId = b.bedId " +
            "WHERE b.buildingNo = #{buildingNo} AND b.floorNo = #{floorNo} AND b.roomNo = #{roomNo}")
    List<DailyDataBaseInfo> screenDailyDataWithAddress(@Param("buildingNo") String buildingNo, @Param("floorNo") String floorNo, @Param("roomNo") String roomNo);

    // 获取最后一条记录的recordId
    @Select("SELECT recordId FROM healthdaily ORDER BY recordId DESC LIMIT 1")
    String getLastRecordId();

    // 插入日常数据
    @Insert("INSERT INTO healthdaily (recordId, elderId, recordDate, temperature, weight, heartRate, mentalState, medication) " +
            "VALUES (#{recordId}, #{elderId}, #{recordDate}, #{temperature}, #{weight}, #{heartRate}, #{mentalState}, #{medication})")
    int insertDailyData(DailyDataDailyData dailyData);

    // 获取最后一条记录的reportNo
    @Select("SELECT reportNo FROM healthcheckup ORDER BY reportNo DESC LIMIT 1")
    String getLastReportNo();

    // 插入体检数据
    @Insert("INSERT INTO healthcheckup (reportNo, elderId, checkDate, bloodPressure, bloodSugar, cholesterol, boneDensity, visionHearing, weight, urineTest, heartRate, mentalState, skinHealth, stomachManagement) " +
            "VALUES (#{reportNo}, #{elderId}, #{checkDate}, #{bloodPressure}, #{bloodSugar}, #{cholesterol}, #{boneDensity}, #{visionHearing}, #{weight}, #{urineTest}, #{heartRate}, #{mentalState}, #{skinHealth}, #{stomachManagement})")
    int insertTijianData(DailyDataTijianData dailyData);
}